<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="所属场站" prop="stationName">
        <el-input
          v-model="queryParams.stationId"
          placeholder="请输入场站名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item  label="所属区域" prop="areaCode">
        <el-select v-model="queryParams.areaCode"  placeholder="请选择">
          <el-option
            v-for="item in jurisdictionUnitData"
            :key="item.areaCode"
            :label="item.areaName"
            :value="item.areaCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="station_btn">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['data:responsibilityNotice:add']"
        >新增</el-button>
      </el-col>
    </el-row>-->

    <el-table v-loading="loading" :data="responsibilityNoticeList" :max-height="tableHeight" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="所属场站" align="left" prop="stationName" width="300"/>
      <el-table-column label="告知书一" align="center" prop="oneNoticePath">
        <template slot-scope="scope">
          <div v-if="scope.row.oneNoticePath != '' && scope.row.oneNoticePath != undefined">
            <el-image :preview-src-list="[scope.row.oneNoticePath]"  :src="scope.row.oneNoticePath" style="width: 70px;height:70px;"></el-image>
          </div>
          <div v-else>暂无图片</div>
        </template>
      </el-table-column>
      <el-table-column label="告知书二" align="center" prop="twoNoticePath">
        <template slot-scope="scope">
          <div v-if="scope.row.twoNoticePath != '' && scope.row.twoNoticePath != undefined">
            <el-image :preview-src-list="[scope.row.twoNoticePath]" :src="scope.row.twoNoticePath" style="width: 70px;height:70px;"></el-image>
          </div>
          <div v-else>暂无图片</div>
        </template>
      </el-table-column>
      <el-table-column label="承诺书" align="center" prop="promisePath">
        <template slot-scope="scope">
          <div v-if="scope.row.promisePath != '' && scope.row.promisePath != undefined">
            <el-image :preview-src-list="[scope.row.promisePath]" :src="scope.row.promisePath" style="width: 70px;height:70px;"></el-image>
          </div>
          <div v-else>暂无图片</div>
        </template>
      </el-table-column>
      <el-table-column label="单位信息采集表" align="center" prop="companyPath">
        <template slot-scope="scope">
          <div v-if="scope.row.companyPath != '' && scope.row.companyPath != undefined">
            <el-image :preview-src-list="[scope.row.companyPath]" :src="scope.row.companyPath" style="width: 70px;height:70px;"></el-image>
          </div>
          <div v-else>暂无图片</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详细</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['data:responsibilityNotice:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['data:responsibilityNotice:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改责任告知书对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px">
      <span>1.<a style="color: blue" href="/profile/files/特种设备安全管理责任告知手册.docx">点击下载</a>《特种设备安全管理责任告知手册》</span><br>
      <span>2.认真阅读手册内容</span><br>
      <span>3.根据手册中附录表打印填写盖章后，按照页面提示上传</span><br>
      <span>4.上传内容有变更时，可以填写盖章后，重新上传</span><br>
      <span>5.只能上传jpg/png/bmp/jpeg文件，且不超过10M</span><br><br>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <label>特种设备安全管理责任告知书</label>
          <div>
            <el-form-item style="float: left;margin-top: 5px" prop="oneNoticePath">
              <el-upload
                :multiple="true"
                accept=".jpg, .png, .gif, .bmp, .jpeg"
                :headers="upload.headers"
                :action="upload.url"
                list-type="picture-card"
                :show-file-list="false"
                :on-success="handleOneNoticePathSuccess"
                :on-progress="handleoneNoticePathProgress"
                :before-upload="beforeAvatarUpload">
                <img v-if="form.oneNoticePath" :src="form.oneNoticePath" :preview-src-list="[form.oneNoticePath]" class="att">
                <el-progress v-if="upload.videoFlag == true" type="circle" :percentage="upload.videoUploadPercent" :width="80" style="margin-top: 16%"></el-progress>
                <div v-if="(form.oneNoticePath == null || form.oneNoticePath == '') &&  upload.videoFlag == false"><i class="el-icon-plus"></i></div>
              </el-upload>
            </el-form-item>
            <el-form-item style="float: left;margin-top: 5px" prop="twoNoticePath">
              <el-upload
                :multiple="true"
                accept=".jpg, .png, .gif, .bmp, .jpeg"
                :headers="upload.headers"
                :action="upload.url"
                list-type="picture-card"
                :show-file-list="false"
                :on-success="handleTwoNoticePathSuccess"
                :on-progress="handleTwoNoticePathProgress"
                :before-upload="beforeAvatarUpload">
                <img v-if="form.twoNoticePath" :src="form.twoNoticePath" :preview-src-list="[form.twoNoticePath]" class="att">
                <el-progress v-if="upload.videoFlag2 == true" type="circle" :percentage="upload.videoUploadPercent2" :width="80" style="margin-top: 16%"></el-progress>
                <div v-if="(form.twoNoticePath == null || form.twoNoticePath == '') && upload.videoFlag2 == false"><i class="el-icon-plus"></i></div>
              </el-upload>
            </el-form-item>
          </div>
          <div style="clear: both"></div>
          <label>无锡市特种设备使用单位安全生产承诺书</label>
          <el-form-item style="margin-top: 5px"  prop="promisePath">
            <el-upload
              :multiple="true"
              accept=".jpg, .png, .gif, .bmp, .jpeg"
              :headers="upload.headers"
              :action="upload.url"
              list-type="picture-card"
              :show-file-list="false"
              :on-success="handlePromisePathSuccess"
              :on-progress="handlePromisePathProgress"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.promisePath" :src="form.promisePath" :preview-src-list="[form.promisePath]" class="att">
              <el-progress v-if="upload.videoFlag3 == true" type="circle" :percentage="upload.videoUploadPercent3" :width="80" style="margin-top: 16%"></el-progress>
              <div v-if="(form.promisePath == null || form.promisePath == '') &&  upload.videoFlag3 == false"><i class="el-icon-plus"></i></div>
            </el-upload>
          </el-form-item>
          <label>特种设备使用单位信息采集表</label>
          <el-form-item style="margin-top: 5px" prop="companyPath">
            <el-upload
              :multiple="true"
              accept=".jpg, .png, .gif, .bmp, .jpeg"
              :headers="upload.headers"
              :action="upload.url"
              list-type="picture-card"
              :show-file-list="false"
              :on-success="handleCompanyPathSuccess"
              :on-progress="handleCompanyPathProgress"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.companyPath" :src="form.companyPath" :preview-src-list="[form.companyPath]" class="att">
              <el-progress v-if="upload.videoFlag4 == true" type="circle" :percentage="upload.videoUploadPercent4" :width="80" style="margin-top: 16%"></el-progress>
              <div v-if="(form.companyPath == null || form.companyPath == '') &&  upload.videoFlag4 == false"><i class="el-icon-plus"></i></div>
            </el-upload>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  详情  -->
    <el-dialog :title="titleDetail" :visible.sync="openView" width="800px">
<!--      <span>1.<a style="color: blue" href="http://www.baudu.com">点击下载</a>《特种设备安全管理责任告知手册》</span><br>-->
<!--      <span>2.认真阅读手册内容</span><br>-->
<!--      <span>3.根据手册中附录表打印填写盖章后，按照页面提示上传</span><br>-->
<!--      <span>4.上传内容有变更时，可以填写盖章后，重新上传</span><br><br>-->
      <el-form ref="form" :model="form" label-width="100px">
        <label>特种设备安全管理责任告知书</label>
        <div>
          <el-form-item style="float: left;margin-top: 5px" prop="oneNoticePath">
            <div v-if="form.oneNoticePath != '' && form.oneNoticePath != undefined">
              <el-image  :src="form.oneNoticePath" :preview-src-list="[form.oneNoticePath]" class="att"></el-image>
            </div>
            <div v-else>暂无图片</div>
          </el-form-item>
          <el-form-item style="float: left;margin-top: 5px" prop="twoNoticePath">
            <div v-if="form.twoNoticePath != '' && form.twoNoticePath != undefined">
              <el-image  :src="form.twoNoticePath" :preview-src-list="[form.twoNoticePath]" class="att"></el-image>
            </div>
            <div v-else>暂无图片</div>
          </el-form-item>
        </div>
        <div style="clear: both"></div>
        <label>无锡市特种设备使用单位安全生产承诺书</label>
        <el-form-item style="margin-top: 5px"  prop="promisePath">
          <div v-if="form.promisePath != '' && form.promisePath != undefined">
            <el-image  :src="form.promisePath" :preview-src-list="[form.promisePath]" class="att"></el-image>
          </div>
          <div v-else>暂无图片</div>
        </el-form-item>
        <label>特种设备使用单位信息采集表</label>
        <el-form-item  style="margin-top: 5px" prop="companyPath">
          <div v-if="form.companyPath != '' && form.companyPath != undefined">
            <el-image  :src="form.companyPath" :preview-src-list="[form.companyPath]" class="att"></el-image>
          </div>
          <div v-else>暂无图片</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResponsibilityNotice, getResponsibilityNotice, delResponsibilityNotice, addResponsibilityNotice, updateResponsibilityNotice, exportResponsibilityNotice } from "@/api/weight/responsibilityNotice";
import { getAreaListByUserId } from "@/api/weight/station";
import { getToken } from "@/utils/auth";

export default {
  name: "ResponsibilityNotice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 责任告知书表格数据
      responsibilityNoticeList: [],
      // 弹出层标题
      title: "",
      titleDetail: "",
      // 是否显示弹出层
      open: false,
      openView: false,
      station_btn: false,
      tableHeight: window.innerHeight - 275,
      srcList: [],
      // 管辖单位
      jurisdictionUnitData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: undefined,
        areaCode: undefined,
      },
      upload: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        videoUploadPercent: 0,
        videoUploadPercent2: 0,
        videoUploadPercent3: 0,
        videoUploadPercent4: 0,
        //  文件个数
        limit: 1,
        isUploading: false,
        videoFlag: false,
        videoFlag2: false,
        videoFlag3: false,
        videoFlag4: false,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const stationId = this.$route.query && this.$route.query.stationId;
    this.queryParams.stationId=stationId;
    this.getList();

    const roles = this.$store.getters && this.$store.getters.roles;
    for (let rol of roles){
      if ('station'==rol || 'hs_station'==rol || 'xq_station'==rol || 'jy_station'==rol || 'yx_station'==rol){
        this.station_btn=true;
      }
    }
    getAreaListByUserId().then(response =>{
      this.jurisdictionUnitData=response.data;
    });
  },
  methods: {
    /** 查询责任告知书列表 */
    getList() {
      this.loading = true;
      listResponsibilityNotice(this.queryParams).then(response => {
        this.responsibilityNoticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stationId: undefined,
        oneNoticePath: undefined,
        twoNoticePath: undefined,
        promisePath: undefined,
        companyPath: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        stationName: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加责任告知书";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getResponsibilityNotice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改责任告知书";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateResponsibilityNotice(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addResponsibilityNotice(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除责任告知书的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delResponsibilityNotice(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有责任告知书数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportResponsibilityNotice(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleOneNoticePathSuccess(response, file) {
      this.upload.videoFlag = false;
      this.upload.videoUploadPercent = 0;
      this.form.oneNoticePath=response.fileName;
    },
    handleoneNoticePathProgress(event, file, fileList){
      this.form.oneNoticePath = "";
      this.upload.videoFlag = true;
      this.upload.videoUploadPercent = Math.floor(event.percent);
    },
    handleTwoNoticePathProgress(event, file, fileList){
      this.form.twoNoticePath = "";
      this.upload.videoFlag2 = true;
      this.upload.videoUploadPercent2 = Math.floor(event.percent);
    },
    handlePromisePathProgress(event, file, fileList){
      this.form.promisePath = "";
      this.upload.videoFlag3 = true;
      this.upload.videoUploadPercent3 = Math.floor(event.percent);
    },
    handleCompanyPathProgress(event, file, fileList){
      this.form.companyPath = "";
      this.upload.videoFlag4 = true;
      this.upload.videoUploadPercent4 = Math.floor(event.percent);
    },
    handleTwoNoticePathSuccess(response, file) {
      this.upload.videoFlag2 = false;
      this.upload.videoUploadPercent2 = 0;
      this.form.twoNoticePath=response.fileName;
    },
    handlePromisePathSuccess(response, file) {
      this.upload.videoFlag3 = false;
      this.upload.videoUploadPercent3 = 0;
      this.form.promisePath=response.fileName;
    },
    handleCompanyPathSuccess(response, file) {
      this.upload.videoFlag4 = false;
      this.upload.videoUploadPercent4 = 0;
      this.form.companyPath=response.fileName;
    },
    beforeAvatarUpload(file) {
      let name = file.name
      let type = name.substring(name.lastIndexOf('.')+1)
      let arry = ["jpg","png","jpeg","bmp","gif"]
      if (arry.indexOf(type) === -1) {
        this.$message.error('文件格式仅支持.jpg, .png, .gif, .bmp, .jpeg格式');
        return false
      }
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10MB!')
        return false
      }
    },
    /** 详情 **/
    handleView(row) {
      getResponsibilityNotice(row.id).then(response => {
        this.form = response.data;
        this.titleDetail = response.data.stationName + '责任告知书详情';
        this.openView = true;
      });
    }
  }
};
</script>
<style>
  .el-upload--picture-card{
    width: 120px;
    height: 120px;
  }
  .el-upload-list--picture-card .el-upload-list__item {
    width: 120px;
    height: 120px;
  }
</style>
<style lang="scss" scoped>
  .att{
    width:120px;height:120px;display: block;
  }
</style>
